﻿#include "WaveEndedLayer.h"
#include "GameUtil.h"
#include "MainGameScene.h"
#include "GameUpgradeScene.h"

using namespace cocos2d::extension;

WaveEndedLayer::~WaveEndedLayer(void)
{
}

bool WaveEndedLayer::init()
{
	if (!CCLayer::init()) {
		return false;  
	}  
	_winSize = CCDirector::sharedDirector()->getWinSize(); 
	
	m_pUiLayer = UILayer::create();
    addChild(m_pUiLayer);
        
    m_pWidget = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("MyGame_04_1.json"));
    m_pUiLayer->addWidget(m_pWidget);
			
	// panel
	UIPanel *root = dynamic_cast<UIPanel*>(m_pUiLayer->getWidgetByName("root"));
	root->setPosition(ccp(_winSize.width/2, _winSize.height/2));
	root->setAnchorPoint(ccp(0.5f, 0.5f));

	UITextButton* button_01 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button01"));
    button_01->addReleaseEvent(this, coco_releaseselector(WaveEndedLayer::toUpdrade));

    UITextButton* button_02 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button02"));
    button_02->addReleaseEvent(this, coco_releaseselector(WaveEndedLayer::nextWave));

	// get data
	const WaveData& data = GameUtil::instance()->getWaveManager()->getWaveData();

	// title 
	UILabel* title = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("title"));
	char tempString[50];
	sprintf(tempString, "wave %d finished~", data.currentWave);
	title->setText(tempString);

	// other data
	time_t t = time(NULL);
	int cost = t - data.startTime;
	int minute = cost / 60;
	int second = cost % 60;
	UILabel* time = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("value01"));
	sprintf(tempString, "%d m %d s", minute, second);
	time->setText(tempString);

	UILabel* money = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("value02"));
	sprintf(tempString, "%d gold", data.getMoney);
	money->setText(tempString);

	UILabel* kill = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("value03"));
	sprintf(tempString, "%d, %d damage", data.kill, data.damage);
	kill->setText(tempString);

	UILabel* wound = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("value04"));
	sprintf(tempString, "%d, %d damage", data.hit, data.getDamage);
	wound->setText(tempString);

		
	UILabel* timeLevel = dynamic_cast<UILabel*>(m_pUiLayer->getWidgetByName("level"));

	// here can make other awards, like finish game fast, give 100 gold
	if(data.hit < 1) {
		timeLevel->setText("Genius~");
	} else if (data.hit < 5) {
		timeLevel->setText("Great~");
	} else if (data.hit < 8) {
		timeLevel->setText("Good~");
	} else {
		timeLevel->setText("HMmmm~");
	}

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/win.mp3", false);
	return true;  
} 

void WaveEndedLayer::nextWave(CCObject* pSender)
{
	//CCLayer* gameLayer = (CCLayer *)this->getParent()->getChildren()->objectAtIndex(0);
	//GameUtil::resume(gameLayer);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();

	this->removeFromParent();
	GameUtil::instance()->getWaveManager()->startWave();
    CCDirector::sharedDirector()->replaceScene(MainGameScene::scene());
}

void WaveEndedLayer::toUpdrade(CCObject* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();

	CCDirector::sharedDirector()->replaceScene(GameUpgradeScene::scene());
}

void WaveEndedLayer::waveEnded(CCScene* scene)
{
	_scene = scene;
	CCLayer* gameLayer = (CCLayer *)scene->getChildren()->objectAtIndex(0);
	GameUtil::pause(gameLayer);

	scene->addChild(this);

	this->setScale(.0f);
	this->runAction(CCScaleTo::create(1.0f, 1.0f));
}